package top.zhangjianyong.tools.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.zhangjianyong.tools.entity.Holiday;
import top.zhangjianyong.tools.factory.HolidayFactory;
import top.zhangjianyong.tools.service.mybatis.IHolidayService;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class HolidayUtil {


    @Autowired
    private IHolidayService iHolidayService;


    public List<Holiday> HolidayListFromApi(Integer year) {
        if (year == null) return null;

        String url = "http://timor.tech/api/holiday/year/" + year + "/";

        log.info(url);
        String res = HttpUtil.get(url);
        log.info(res);
        JSONObject parseContext = JSONObject.parseObject(res);
        //获取json数据里的data数组
        JSONObject holidayMap = parseContext.getJSONObject("holiday");

        return holidayMap.values().stream().map(o -> {
            JSONObject json = (JSONObject) o;
            return HolidayFactory.create(json);
        }).sorted(Comparator.comparing(Holiday::getDate)).collect(Collectors.toList());

    }

    public boolean isWorkDay(LocalDate date) {

        Holiday holiday = iHolidayService.getHolidayByDate(date);
        if (holiday != null) {
            if (holiday.getIsHoliday()) {
                log.info("假期，今天不上班");
                return false;
            } else {
                log.info("假期，今天上班");
                return true;
            }
        } else {
            int dayOfWeek = date.getDayOfWeek().getValue();
            if (dayOfWeek == 6 || dayOfWeek == 7) {
                log.info("周末，今天不上班");
                return false;
            } else {
                log.info("工作日，今天上班");
                return true;
            }
        }
    }

    public boolean isStock(LocalDate date) {
        int dayOfWeek = date.getDayOfWeek().getValue();
        if (dayOfWeek == 6 || dayOfWeek == 7) {
            log.info("周末，休市");
            return false;
        }
        Holiday holiday = iHolidayService.getHolidayByDate(date);
        if (holiday != null) {
            if (holiday.getIsHoliday()) {
                log.info("假期，休市");
                return false;
            }
        }
        return true;
    }

    public LocalDate lastOrCurrentStock(LocalDate date) {
        if (isStock(date)) return date;
        date = date.plusDays(-1);
        return lastOrCurrentStock(date);
    }

    public LocalDate lastStock(LocalDate date) {
        date = date.plusDays(-1);
        if (isStock(date)) return date;
        return lastStock(date);
    }

    public List<LocalDate> lastStock(LocalDate date, int days) {
        List<LocalDate> dates = new ArrayList<>();
        for (int i = 0; i < days; i++) {
            date = lastStock(date);
            dates.add(date);
        }
        return dates;
    }

    public LocalDate nextStock(LocalDate date) {
        date = date.plusDays(1);
        if (isStock(date)) return date;
        return lastStock(date);
    }


}
